package org.skydiveapplibrary.ui;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public abstract class BaseListAdapter<T> extends BaseAdapter
{
	private Context context;
	private List<T> items;
	
	public BaseListAdapter(
			Context context,
			List<T> items)
	{
		this.context = context;
		this.items = items;
	}
	
	@Override
	public int getCount()
	{
		return items.size();
	}

	@Override
	public Object getItem(int index)
	{
		return items.get(index);
	}
	
	@Override
	public long getItemId(int index)
	{
		return index;
	}
	
	public int indexOf(T item)
	{
		return items.indexOf(item);
	}
	
	@Override
	public View getView(int index, View view, ViewGroup parent)
	{
		return getTextView(index, view, parent,
				android.R.layout.simple_list_item_1);
	}
	
	protected Context getContext()
	{
		return context;
	}
	
	public List<T> getItems()
	{
		return items;
	}
	
	protected abstract String getText(T item);
	
	protected View getTextView(int index, View view, ViewGroup parent, int textViewResourceId)
	{
		if (view == null)
		{
			// get view
			view = View.inflate(
					context,
					textViewResourceId,
					null);
		}
		
		// set text
		TextView tv = (TextView)view;
		String text = getText(items.get(index));
		tv.setText(text);
		
		return view;
	}
}
